package mfuny.ggongal.admin.winner;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import mfuny.ggongal.admin.Server;
import mfuny.ggongal.admin.play.PlayService;

import org.springframework.web.servlet.ModelAndView;

import com.ibatis.sqlmap.client.SqlMapClient;

public class WinnerService {
	SqlMapClient sqlMapper;
	ModelAndView mav;
	
	public WinnerService() throws Exception {
		sqlMapper = Server.getSqlMapper();
		mav = new ModelAndView();
	}
	
	//이벤트 결과 페이지.
	public ModelAndView winnerResult() throws Exception {
		mav.clear();
		mav.setViewName("/winner/winnerResult.ga");
		return mav;
	}
	
	//이벤트 결과 디테일.
	@SuppressWarnings({ "unchecked", "deprecation" })
	public ModelAndView winnerResult_detail(int ad_no) throws Exception {
		HashMap<String, Object> resultMap = (HashMap<String, Object>) sqlMapper.queryForObject("ad_selectAllOne2", ad_no);
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		
		String temp = (String)resultMap.get("days");
		String[] days = temp.split(",");
		for(int i = 0; i < days.length; i++) {
			String[] temp1 = days[i].split("/");
			String[] tempDay = temp1[0].split("-");
			
			Date date = new Date();
			date.setYear(Integer.parseInt(tempDay[0]) - 1900);
			date.setMonth(Integer.parseInt(tempDay[1]) - 1);
			date.setDate(Integer.parseInt(tempDay[2]));
			date.setHours(Integer.parseInt(temp1[1]));
			date.setMinutes(0);
			date.setSeconds(0);
			
			HashMap<String, Object> map = (HashMap<String, Object>) resultMap.clone();
			map.put("date", date);
			map.put("timeToPlay", days[i]);
			if(date.getTime() < System.currentTimeMillis() && date.getTime() > System.currentTimeMillis() - 3600000 ) {
				map.put("status", "진행중");
			} else if(date.getTime() < System.currentTimeMillis()) {
				map.put("status", "종료");
			} else if(date.getTime() > System.currentTimeMillis() ) {
				map.put("status", "진행대기");
			}
			list.add(map);
		}
		
		for(int i = 0; i < list.size() - 1; i++) {
			for(int j = i+1; j < list.size(); j++) {
				HashMap<String, Object> map_i = list.get(i);
				HashMap<String, Object> map_j = list.get(j);
				if(((Date)map_i.get("date")).getTime() < ((Date)map_j.get("date")).getTime()) {
					list.set(i, map_j);
					list.set(j, map_i);
				}
			}
		}
		
		mav.clear();
		mav.setViewName("/winner/winnerResult_detail");
		mav.addObject("resultList", list);
		return mav;
	}
	
	//당첨자 리스트 팝업.
	@SuppressWarnings({ "deprecation", "unchecked" })
	public ModelAndView winnerListPopup(int ad_no, String days) throws Exception {
		String[] day1 = days.split("/");
		String[] day2 = day1[0].split("-");
		
		Date date = new Date();
		date.setYear(Integer.parseInt(day2[0]) - 1900);
		date.setMonth(Integer.parseInt(day2[1]) - 1);
		date.setDate(Integer.parseInt(day2[2]));
		date.setHours(Integer.parseInt(day1[1]));
		date.setMinutes(0);
		date.setSeconds(0);
		
		//당첨자 리스트.
		List<Object> winnerList = new PlayService().getWinnerForAd(ad_no, date);
		
		HashMap<String, Object> param = new HashMap<String, Object>();
		Timestamp gamestarttime = new Timestamp(date.getTime());
		Timestamp gameendtime = (Timestamp) gamestarttime.clone();
		gamestarttime.setNanos(0);
		gameendtime.setHours(gameendtime.getHours() + 1);
		gameendtime.setNanos(0);
		param.put("gamestarttime", gamestarttime);
		param.put("gameendtime", gameendtime);
		param.put("ad_no", ad_no);
		
		//참여자 타겟별 리포트.
		List<HashMap<String, Object>> targetReport = sqlMapper.queryForList("user_joinGameReport2", param);
		
		//종류별로 분리.
		List<HashMap<String, Object>> ageList = new ArrayList<HashMap<String, Object>>();
		List<HashMap<String, Object>> areaList = new ArrayList<HashMap<String, Object>>();
		List<HashMap<String, Object>> sexList = new ArrayList<HashMap<String, Object>>();
		List<HashMap<String, Object>> marryList = new ArrayList<HashMap<String, Object>>();
		List<HashMap<String, Object>> jobList = new ArrayList<HashMap<String, Object>>();
		
		for(int i = 0; i < targetReport.size(); i++) {
			HashMap<String, Object> map = targetReport.get(i);
			String t_type = map.get("t_type").toString();
			if(t_type.equals("age")) {
				ageList.add(map);
			}else if(t_type.equals("area")) {
				areaList.add(map);
			}else if(t_type.equals("sex")) {
				sexList.add(map);
			}else if(t_type.equals("marry")) {
				marryList.add(map);
			}else if(t_type.equals("job")) {
				jobList.add(map);
			}
		}
		
		mav.clear();
		mav.setViewName("/winner/winnerListPopup");
		mav.addObject("winnerList", winnerList);
		mav.addObject("targetReport", targetReport);
		mav.addObject("ageList", ageList);
		mav.addObject("areaList", areaList);
		mav.addObject("sexList", sexList);
		mav.addObject("marryList", marryList);
		mav.addObject("jobList", jobList);
		
		return mav;
	}
}
